<template>
    <div>
        <Row class="formRow">
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle" style="width: 100%">产品类型名称</span>
          </Col>
          <Col span="3" class="leftRow">
           <Input v-model="formInline.prdTypeName" placeholder="产品类型名称" clearable style="width: 100%" size="large"></Input>
          </Col>
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle">是否有效</span>
          </Col>
          <Col span="2" class="leftRow">
            <Select  size="large" style="width:100%" v-model="formInline.valid">
              <Option  value="1" >是</Option>
              <Option  value="0" >否</Option>
            </Select>
          </Col>
          <Col span="15" class="leftRow">
            <Button type="primary" @click="onSubmit">查询</Button>
            <Button type="primary" @click="resetForm">重置</Button>
            <Button type="primary" @click="onAdd">添加</Button>
          </Col>
        </Row>

      <Table :data="tableData" :columns="cloums" stripe :loading="loading"  style="margin-top: 20px"></Table>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page :total="total" :current="1" :placement="placement" @on-change="changePage" show-total show-elevator show-sizer @on-page-size-change="changeSize"></Page>
        </div>
      </div>

      <Modal
        v-model="add"
        title="添加"
        @on-ok="addok"
        @on-cancel="cancel('您取消了操作')" width="600">
        <Row>
          <Col span="15">   <span class="ModelInputTitle" style="width: 130px"><Icon type="ios-medical" color="red"></Icon>产品类型名称</span><Input v-model="addForm.prdTypeName" style="width: 180px"></Input></Col>
          <Col span="9">   <span class="ModelInputTitle">是否有效</span> <Select   v-model="addForm.valid" style="width: 100px">
          <Option  value="1" >是</Option>
          <Option  value="0" >否</Option>
        </Select></Col>
        </Row>
      </Modal>

      <Modal
        v-model="edit"
        title="编辑"
        @on-ok="editok"
        @on-cancel="cancel('您取消了操作')" width="600">
        <Row>
          <Col span="15">   <span class="ModelInputTitle" style="width: 130px"><Icon type="ios-medical" color="red"></Icon>产品类型名称</span><Input v-model="editForm.prdTypeName" style="width: 180px"></Input></Col>
          <Col span="9">   <span class="ModelInputTitle">是否有效</span> <Select   v-model="editForm.valid" style="width: 100px">
          <Option  value="1" >是</Option>
          <Option  value="0" >否</Option>
        </Select></Col>
        </Row>
      </Modal>
    </div>
</template>
<script type="text/ecmascript-6">
  import axios from  "axios";
    export default {
        data() {
            return {
              formInline:{
                valid:"",
                prdTypeName:""
              },
              add:false,
              addForm:{
                valid:"",
                prdTypeName:""
              },
              edit:false,
              editForm:{
                valid:"",
                prdTypeName:"",
                id:""
              },
              page:1,
              pageSize:10,
              placement:"bottom",
              loading:true,
              total:0,
              tableData:[],
              cloums:[
                {
                  title: '产品类型名称',
                  key: 'prdTypeName',
//                  width:200,
                  align:"center"
                },
                {
                  title: '是否有效',
                  key: 'valid',
                  width:200,
                  align:"center"
                },
                {
                  title: '创建人',
                  key: 'creator',
//                  width:200,
                  align:"center"
                },
                {
                  title: '修改人',
                  key: 'updater',
//                  width:200,
                  align:"center"
                },{
                  title: '操作',
                  width:200,
                  align:"center",
                  fixed:"right",
                  render:(h,params)=>{
                    return h("div",[
                      h("Button",{
                        props:{
                          type:"primary"
                        },
                        on:{
                          click:()=>{
                            console.log(params.row)
                            this.edit = true;
                                this.editForm.prdTypeName = params.row.prdTypeName;
                                this.editForm.id = params.row.id;
                                this.editForm.valid = params.row.valid=="是"?"1":"0";
                          }
                        }
                      },"编辑")
                    ])
                  }
                }
              ]
            }
        },
      mounted(){
          this.init()
      },
      methods:{
        init(){
          var qs = require("qs");
          var param = qs.stringify({
            page:this.page,
            pageSize:this.pageSize,
            id:"",
            prdTypeName:this.formInline.prdTypeName,
            valid:this.formInline.valid
          })
          var _this = this;
          axios.defaults.headers.common['Authorization'] = sessionStorage.getItem("token");
          axios.post("/productType/selectProductTypePage",param).then((res)=>{
              if(res.data.code=="000000"){
                _this.loading = false;
                _this.total = res.data.count;
                for(let i=0;i<res.data.data.length;i++){
                  res.data.data[i].valid = res.data.data[i].valid=="1"?"是":"否";
                }
                _this.tableData = res.data.data;
              }else{
                _this.$Notice.error({title:res.data.msg});
              }
          })
        },
        onSubmit(){
          this.init()
        },
        resetForm(){
          this.formInline.valid = "";
          this.formInline.prdTypeName = "";
        },
        onAdd(){
          this.add = true;
        },
        changePage(page){
          this.page = page;
          this.loading = true;
          this.init();
        },
        changeSize(size){
          this.placement = size>=20?"top":"bottom";
          this.pageSize = size;
          this.loading = true;
          this.init();
        },
        cancel (msg) {
          this.$Notice.info({title:msg});
        },
        addok(){
          var qs = require("qs");
          var param = qs.stringify({
            prdTypeName:this.addForm.prdTypeName,
            valid:this.addForm.valid
          })
          var _this = this;
          axios.post("/productType/saveProductType",param).then((res)=>{
            if(res.data.code=="000000"){
              _this.$Notice.success({title:"添加成功"});
              _this.init();
            }else{
              _this.$Notice.error({title:res.data.msg});
            }
          })
        },
        editok(){
          var qs = require("qs");
          var param = qs.stringify({
            prdTypeName:this.editForm.prdTypeName,
            valid:this.editForm.valid,
            id:this.editForm.id
          })
          var _this = this;
          axios.post("/productType/updateProductType",param).then((res)=>{
            if(res.data.code=="000000"){
              _this.$Notice.success({title:"修改成功"});
              _this.init();
            }else{
              _this.$Notice.error({title:res.data.msg});
            }
          })
        }
      }
    }
</script>
